import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;


public class TicTacRemoteObject extends RemoteObject implements TicTacRemoteInterface {
	
	protected TicTacRemoteObject(String me) throws RemoteException {
		super(me);
	}
	
	protected TicTacRemoteObject(String me, NetworkEventListener cl) throws RemoteException {
		super(me, cl);
	}
	
	public static TicTacRemoteObject createTicTacRIObject(String me, NetworkEventListener cl){
		try {
			return new TicTacRemoteObject(me, cl);
		} catch (RemoteException e) {
			System.out.println("FAILED: Could not create a TicTacRemoteInterface Object");
			e.printStackTrace();
		}
		return null;
	}


	@Override
	public void receivedMove(int row, int col) throws RemoteException {
		NetworkEvent event = new MoveEvent(row, col);
		cl.event(event);
	}
	

	public void disconnect() throws RemoteException {
		NetworkEvent event = new DisconnectEvent();
		cl.event(event);
	}
	
	
	public static RemoteInterface lookupRIObject(String id, Registry registry) throws RemoteException {
		RemoteInterface remoteInterface = new RemoteObject("nil");
		try {
			System.out.print("[Fetching Object] ");
			remoteInterface = (RemoteInterface) registry.lookup(id);
			System.out.println("SUCCESS: Remote Object Detected");
		} catch (AccessException e) {
			System.out.println("FAILED: You do not have access to the object");
			e.printStackTrace();
		} catch (RemoteException e) {
			System.out.println("FAILED: Some remote exception");
			e.printStackTrace();
		}  catch (NotBoundException e) {
			System.out.println("FAILED: Could not find the remote object you were looking for");
			e.printStackTrace();
		}
		return remoteInterface;
	}
	
}
